package com.blockchain.front.servlets.trading.ajax;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.trading.TradingManage;
import com.blockchain.front.servlets.trading.AbstractTradingServlet;
import com.blockchain.front.util.JsonUtil;
import com.blockchain.util.parser.BigDecimalParser;
import com.blockchain.util.parser.IntegerParser;

public class TradingMr extends AbstractTradingServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		
		PrintWriter out = response.getWriter();
		int scid = IntegerParser.parse(request.getParameter("scid"));
		BigDecimal price = BigDecimalParser.parse(request.getParameter("price"));
		BigDecimal num = BigDecimalParser.parse(request.getParameter("num"));
		String password = request.getParameter("pwtrade");
		TradingManage tradingManage = serviceSession.getService(TradingManage.class);
		Map<String,Object> result  = new HashMap<>();
		result.put("num", "0");
		result.put("msg", "");
		if (scid <= 0) {
			result.put("msg", "市场异常");
			out.write(JsonUtil.GsonString(result));
			return;
		}
		try {
			tradingManage.tradingMr(scid, price, num, password);
			//向撮合系统增加撮合记录
			/*MakeMatch match = (MakeMatch)request.getServletContext().getAttribute("CHUANGKE_MATCH");
			if(null!=match) {
				String[] params = new String[8];
				params[0]=String.valueOf(id);
				params[1] = String.valueOf(scid);
				params[2] = String.valueOf(serviceSession.getSession().getAccountId());
				params[3] = String.valueOf(price);
				params[4] = String.valueOf(num);
				params[5] = "0";
				params[6] = String.valueOf(System.currentTimeMillis());
				params[7] = "0";
				match.appendBuy(match.genBuy(params));
				match.appendBuyOrSellId(id);
			}*/
			result.put("num", "1");
			result.put("msg", "");
			out.write(JsonUtil.GsonString(result));
		} catch (Throwable e) {
			result.put("msg", e.getMessage());
			e.printStackTrace();
			out.write(JsonUtil.GsonString(result));
			return;
		}
		
	}
}
